//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Eas.WebCrawler.Test.PlugIns {
    /// <summary>
    /// Summary description for HtmlLinkExtractorTest
    /// </summary>
    [TestClass]
    public class HtmlLinkExtractorTest {

        private string TestFilesDir = @"D:\Dev\Proj\Eas.WebCrawler\TestFiles";

        public HtmlLinkExtractorTest() {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        IEnumerable<int> GetInts() {
            yield return 1;
            yield return 5;
            yield return 8;
            yield return 3;
            yield return 7;
        }

        /// <summary>The TestSimpleLinkFile test.</summary>
        [TestMethod]
        public void TestSimpleLinkFile() {
            using (System.IO.TextReader reader = new System.IO.StreamReader(System.IO.Path.Combine(TestFilesDir, "test.html"))) {
                Eas.WebCrawler.PlugIns.LinkExtractors.HtmlLinkExtractor extractor = new Eas.WebCrawler.PlugIns.LinkExtractors.HtmlLinkExtractor();
                extractor.Initialize(null);
                IEnumerator<Uri> results = extractor.GetLinks(null, reader).GetEnumerator();
                CompareUri(results, "http://www.gong.com/ssss.aspx");
                CompareUri(results, "relative.aspx", true);
                CompareUri(results, "relative\\directory", true);
                CompareUri(results, "directory", true);
                CompareUri(results, "http://www.tiscali.de");
                CompareUri(results, "anotherRelativeLink.aspx", true);
                Assert.IsFalse(results.MoveNext());
            }
        }

        [TestMethod]
        /// <summary>Executes the TestNoLinks test.</summary>
        public void TestNoLinks() {
            using (System.IO.TextReader reader = new System.IO.StreamReader(System.IO.Path.Combine(TestFilesDir, "HelloWorld.html"))) {
                Eas.WebCrawler.PlugIns.LinkExtractors.HtmlLinkExtractor extractor = new Eas.WebCrawler.PlugIns.LinkExtractors.HtmlLinkExtractor();
                IEnumerator<Uri> results = extractor.GetLinks(null, reader).GetEnumerator();
                Assert.IsFalse(results.MoveNext());
            }
        }


        [TestMethod]
        /// <summary>Executes the TestNoGroupLinkExtractor test.</summary>
        public void TestNoGroupLinkExtractor() {
            using (System.IO.TextReader reader = new System.IO.StreamReader(System.IO.Path.Combine(TestFilesDir, "test.html"))) {
                Eas.WebCrawler.PlugIns.LinkExtractors.RegularExpressionLinkExtractor e =
                    new Eas.WebCrawler.PlugIns.LinkExtractors.RegularExpressionLinkExtractor(new Eas.WebCrawler.PlugIns.LinkExtractors.RegexSpecification("http://www.gong.com/ssss.aspx"));
                IEnumerator<Uri> results = e.GetLinks(null, reader).GetEnumerator();
                CompareUri(results, "http://www.gong.com/ssss.aspx");
                Assert.IsFalse(results.MoveNext());
            }
        }


        private void CompareUri(IEnumerator<Uri> enumerator, string uri) {
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual<Uri>(new Uri(uri), enumerator.Current);
        }

        private void CompareUri(IEnumerator<Uri> enumerator, string uri, bool isRelative) {
            Assert.IsTrue(enumerator.MoveNext());
            Uri u;
            Assert.IsTrue(Uri.TryCreate(uri, isRelative ? UriKind.Relative : UriKind.Absolute, out u));
            Assert.AreEqual<Uri>(u, enumerator.Current);
        }

        [TestMethod]
        /// <summary>Executes the TestGeneratingASpecification test.</summary>
        public void TestGeneratingASpecification() {
            Eas.WebCrawler.PlugIns.LinkExtractors.RegexSpecification spec = new Eas.WebCrawler.PlugIns.LinkExtractors.RegexSpecification("w+", "erik");
            Assert.AreEqual<string>("w+", spec.Expression.ToString());
            Assert.AreEqual<string>("erik", spec.GroupName);
        }


    }
}
